package com.xiaoyushu.xiaoyushu.data.align.job;


import com.xiaoyushu.xiaoyushu.data.align.constant.TableConstants;
import com.xiaoyushu.xiaoyushu.data.align.domain.mapper.CreateTableMapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 定时任务:自动创建日增量   计数变更表
 */
@Component
@RefreshScope
public class CreateTableXxlJon {


    @Value("${table.shards}")
    private int tableShards;

    @Resource
    private CreateTableMapper createTableMapper;

    @XxlJob("createTableJobHandler")
    public void createTableJobHandler() throws Exception{
        //表后缀
        String date= LocalDate.now().plusDays(1) //明日的日期
                .format(DateTimeFormatter.ofPattern("yyyyMMdd"));//转字符串

        XxlJobHelper.log("## 开始初始化明日增量数据表,日期:{}....",date);

        if (tableShards>0){
            for (int hashKey=0;hashKey<tableShards;hashKey++){
                //表名 后缀
                String tableNameSuffix = TableConstants.buildTableNameSuffix(date, hashKey);

                //创建表
                // 创建表
                createTableMapper.createDataAlignFollowingCountTempTable(tableNameSuffix);
                createTableMapper.createDataAlignFansCountTempTable(tableNameSuffix);
                createTableMapper.createDataAlignNoteCollectCountTempTable(tableNameSuffix);
                createTableMapper.createDataAlignUserCollectCountTempTable(tableNameSuffix);
                createTableMapper.createDataAlignUserLikeCountTempTable(tableNameSuffix);
                createTableMapper.createDataAlignNoteLikeCountTempTable(tableNameSuffix);
                createTableMapper.createDataAlignNotePublishCountTempTable(tableNameSuffix);
            }
        }

        XxlJobHelper.log("## 结束创建 日增量数据表,日期:{}....",date);

    }

}
